﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace USTF.Media.MediaService.UmbracoDefault
{
    public static class UmbracoMediaMapping
    {
        public static MediaItem ToMediaItem(this umbraco.cms.businesslogic.media.Media umbracoMediaItem)
        {
            var result = new MediaItem
            {
                ID = umbracoMediaItem.Id
            };

            result.Properties.Add("ContentTypeAlias",
                (umbracoMediaItem.ContentType == null) ? "folder" : umbracoMediaItem.ContentType.Alias);
            result.Properties.Add(CommonFieldNames.TITLE, umbracoMediaItem.Text);
            switch (umbracoMediaItem.ContentType.Alias.ToLower().Trim()) {
                case "image":
                    result.MediaType = MediaType.Image;
                    var fileProp = umbracoMediaItem.getProperty("umbracoFile");
                    if (fileProp != null) {
                        string fileName = umbracoMediaItem.getProperty("umbracoFile").Value.ToString().Split('/').Last();
                        result.Properties.Add(CommonFieldNames.FILENAME, fileName);
                    }
                    break;
                default:
                case "folder":
                    result.MediaType = MediaType.Folder;
                    break;
            }

            return result;
        }
    }
}
